package fr.inserm.u915.organifooyi.model;



import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Transient;

import fr.inserm.u915.organifooyi.lang.Cast;
import fr.inserm.u915.organifooyi.lang.Equals;
import fr.inserm.u915.organifooyi.web.Message;

@Entity(name="aliquot")
@Table(name="aliquot")
@NamedQueries({
    @NamedQuery(name="aliquot.list.all",
            query="select A from aliquot as A ")
   	})
public class Aliquot
	extends AbstractUpdatableObject
	{
	@Column(name="aliquotIndex")
	private Integer Index;
	@ManyToOne(fetch=FetchType.LAZY)
	@JoinColumn(name="extraction_id")
	private Extraction extraction;
	
	@Column(name="volume")
	private Double volume;
	
	@Column(name="validGel")
	private Short gel;
	
	@Column(name="concPicoGreen")
	private Double concPicoGreen;
	
	@Column(name="ratio_260_230")
	private Double ratio260230;
	
	@Column(name="ratio_260_280")
	private Double ratio260280;

	@Column(name="concNanoDrop")
	private Double concNanoDrop;
	
	
	
	public Integer getIndex() 
		{
		return Index;
		}
	public void setIndex(Integer index) {
		Index = index;
		}
	public Extraction getExtraction() {
		return extraction;
		}
	public void setExtraction(Extraction extraction) {
		this.extraction = extraction;
		}
	
	
	
	
	public Double getVolume() {
		return volume;
	}
	public void setVolume(Double volume) {
		this.volume = volume;
	}
	public Short getGel() {
		return gel;
	}
	public void setGel(Short gel) {
		this.gel = gel;
	}
	public Double getConcPicoGreen() {
		return concPicoGreen;
	}
	public void setConcPicoGreen(Double concPicoGreen) {
		this.concPicoGreen = concPicoGreen;
	}
	public Double getRatio260230() {
		return ratio260230;
	}
	public void setRatio260230(Double ratio260230) {
		this.ratio260230 = ratio260230;
	}
	public Double getRatio260280() {
		return ratio260280;
	}
	public void setRatio260280(Double ratio260280) {
		this.ratio260280 = ratio260280;
	}
	public Double getConcNanoDrop() {
		return concNanoDrop;
	}
	public void setConcNanoDrop(Double concNanoDrop) {
		this.concNanoDrop = concNanoDrop;
	}
	
		
	@Override
	public boolean equals(Object obj)
		{
		if(obj==this) return true;
		if(obj==null || obj.getClass()!=this.getClass()) return false;
		return Equals.equals(getId(),Aliquot.class.cast(obj).getId());
		}
	
	@Transient
	public String getLabel()
		{
		return getExtraction()==null?"Aliquot ID."+getId():getExtraction().getLabel()+"_"+getIndex();
		}
	
	@Override
	public String toString()
		{
		return getLabel();
		}
	
	
	public Status updateVolume(String literal)
		{
		Double dbl=Cast.cast(literal, Double.class);
		if(dbl==null)
			{
			return new Status(new Message("volume: Cannot cast \""+literal+"\" to Double",Message.Level.error));
			}
		if(dbl<0)
			{
			return new Status(new Message("volume: \""+literal+"\" should be greater or equal to zero",Message.Level.error));
			}
		if(!Equals.equals(getVolume(), dbl))
			{
			Double oldvalue=getVolume();
			setVolume(dbl);
			
			return new Status(
					new History(DataType.aliquot,getId(),getVersion(),"volume","Double",
					String.valueOf(oldvalue),
					String.valueOf(dbl),
					null
					));
			}
		return  new Status(new Message("OK",Message.Level.success));
		}
	
	public Status updateConcNanoDrop(String literal)
		{
		Double dbl=Cast.cast(literal, Double.class);
		if(dbl==null)
			{
			return new Status(new Message("concNanoDrop: Cannot cast \""+literal+"\" to Double",Message.Level.error));
			}
		if(dbl<0)
			{
			return new Status(new Message("concNanoDrop: \""+literal+"\" should be greater or equal to zero",Message.Level.error));
			}
		if(!Equals.equals(getVolume(), dbl))
			{
			Double oldvalue=getConcNanoDrop();
			setConcNanoDrop(dbl);
			
			return new Status(
					new History(DataType.aliquot,getId(),getVersion(),
					"concNanoDrop","Double",
					String.valueOf(oldvalue),
					String.valueOf(dbl),
					null
					));
			}
		return  new Status(new Message("OK",Message.Level.success));
		}
	
	
	public Status update(String fieldName,String literal)
		{
		if(fieldName.equals("volume"))
			{
			return updateVolume(literal);
			}
		else if(fieldName.equals("concNanoDrop"))
			{
			return updateConcNanoDrop(literal);
			}
		throw new IllegalArgumentException(fieldName);
		}
	}

